package Gui;

import java.awt.*;
import javax.swing.*;

import javax.imageio.ImageIO;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import Logic.LoginInfo;
import client.ClientHendler;
import client.LibaryMain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;


/**
 * this class implements gui of login window 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class Login extends JPanel {
	
	private JTextField usertextField;
	private JPasswordField passtextField;
	private JTextField PortTextField;
	private JTextField IPTextField;
	static LoginInfo loginInfo=null;
	static String un=null;
	static String pas=null;
	private ClientHendler client;
	
	/**
	 * creates the login window 
	 * @throws IOException
	 */
		public Login() throws IOException{
			
			
			initComponents();
		}
		
		
		public void initComponents() throws IOException{
			
			
			setBackground(new Color(153, 204, 255));
			setSize(945,611);

			JLabel lblUserId = new JLabel("User ID");
			lblUserId.setBounds(345, 298, 57, 19);
			lblUserId.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JLabel lblPassword = new JLabel("Password");
			lblPassword.setBounds(345, 340, 73, 19);
			lblPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JLabel lblPort = new JLabel("Port");
			lblPort.setBounds(345, 382, 32, 19);
			lblPort.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JLabel lblIp = new JLabel("IP");
			lblIp.setBounds(345, 427, 17, 19);
			lblIp.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JButton btnExit = new JButton("EXIT");
			btnExit.setIcon(new ImageIcon(Login.class.getResource("/Gui/images/cancel.gif")));
			btnExit.setBounds(810, 553, 116, 44);
			btnExit.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
			btnExit.addActionListener(new java.awt.event.ActionListener() {     // Login action listener 
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	btnExitActionPerformed(evt);
	            
	            }

				private void btnExitActionPerformed(ActionEvent evt) {
					System.exit(0);
					
				}
	        });
			
			JButton btnLogin = new JButton("Login");
			btnLogin.setIcon(new ImageIcon(Login.class.getResource("/Gui/images/finish.gif")));
			btnLogin.setBounds(345, 471, 257, 36);
			btnLogin.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			btnLogin.addActionListener(new java.awt.event.ActionListener() {     // Login action listener 
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	try {
						btnLoginActionPerformed(evt);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            
	            }
	        });
			
			usertextField = new JTextField();
			usertextField.setBounds(486, 297, 116, 22);
			usertextField.setColumns(10);
			
			PortTextField = new JTextField();
			PortTextField.setBounds(486, 381, 116, 22);
			PortTextField.setColumns(10);
			
			IPTextField = new JTextField();
			IPTextField.setBackground(new Color(255, 255, 255));
			IPTextField.setBounds(486, 426, 116, 22);
			IPTextField.setColumns(10);
			
			passtextField = new JPasswordField();
			passtextField.setBounds(486, 339, 116, 22);
			setLayout(null);
			add(btnExit);
			add(btnLogin);
			add(lblUserId);
			add(lblPort);
			add(lblIp);
			add(lblPassword);
			add(IPTextField);
			add(PortTextField);
			add(usertextField);
			add(passtextField);
			
			JLabel lblLogo = new JLabel("LOGO");
			lblLogo.setIcon(new ImageIcon(Login.class.getResource("/Gui/images/Logo.png")));
			lblLogo.setFont(new Font("Tahoma", Font.BOLD, 82));
			lblLogo.setBounds(241, 56, 462, 213);
			add(lblLogo);
			
			JButton btnSearch = new JButton("Search In Library");
			btnSearch.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			btnSearch.setBounds(52, 552, 192, 44);
			add(btnSearch);	
			btnSearch.addActionListener(new ActionListener() {
		         public void actionPerformed(java.awt.event.ActionEvent evt) {
		            	try {
							btnSearchActionPerformed(evt);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		            
					}
		        });
		}
		protected void btnSearchActionPerformed(ActionEvent evt) throws IOException {
			DirectorWindow.roll="Login";
			int Port= 0;
			String IP= "";
			

			if (PortTextField.getText().equals("")) Port= 5555;  // define port 
			else Port = Integer.parseInt(PortTextField.getText());
			
			if (IPTextField.getText().equals("")) IP="127.0.0.1"; // define ip 
			else IP= IPTextField.getText();
			

			IP= IPTextField.getText();
		
			
			if (PortTextField.getText().equals("")) 
			{
				JOptionPane.showMessageDialog(null, "Please enter port ");
					
			}
			
			
			else if (IP.equals("")) 
			{
				JOptionPane.showMessageDialog(null, "Please enter ip ");
					
			}
			
	
			
			else	if (    Port!= 0  && (!( IP.equals("")))    )
			{
		
			
			LibaryMain.port= Port;
			LibaryMain.host= IP;
			
	
			 this.client = new ClientHendler( IP , Port); /// you need to enter real parameters this is onlt patch to you computer
		     LibaryMain.chat = client;

			Search sm= new Search();
			MainScreen.card.add("Search", sm);
			MainScreen.cardLayot.show(MainScreen.card, "Search");
			}
			
		}
		
		private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) throws IOException { 
			 int Port= 0;
			String IP= null;
			
			
			String userName =usertextField.getText();  //get user name text field 
			String password =passtextField.getText();  //get password text field 
			
			if (userName.equals("") ) 
			{
				JOptionPane.showMessageDialog(null, "Please enter a user name ");
					
			}
			
			else if (password.equals("") ) 
			{
				JOptionPane.showMessageDialog(null, "Please enter a password");
				
			}
			
			
			else	if (    (!(userName.equals(""))) || (!( password.equals("")))    )
					{
			loginInfo = new LoginInfo(userName , password ); // save login information
			un=userName;
			pas=password;
			
			
			
			if (PortTextField.getText().equals("")) Port= 5555;  // define port 
			else Port = Integer.parseInt(PortTextField.getText());
			
			if (IPTextField.getText().equals("")) IP="127.0.0.1"; // define ip 
			else IP= IPTextField.getText();
			
			LibaryMain.port= Port;
			LibaryMain.host= IP;
			
			
			
		
		    
		    
			ArrayList<String> Arr=new ArrayList<String>();
	        Arr.add("Login");
	        Arr.add(userName);
	        Arr.add(password);
	        this.client = new ClientHendler( IP , Port); 
		    LibaryMain.chat = client;
	       
	        client.handleMessageFromGui(Arr);
			Login.this.setVisible(false);
			
			
				}
		}

/**
 * this method get the client from the client hendler 
 * @return the client 
 */
		public ClientHendler getClient() {
			return client;
		}

/**
 * this method get the client from the client hendler
 * @param client
 */
		public void setClient(ClientHendler client) {
			this.client = client;
		}
}